package cn.lhl.service.system.impl;

import cn.lhl.dao.system.RoleDao;
import cn.lhl.domain.system.Module;
import cn.lhl.domain.system.Role;
import cn.lhl.service.system.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    //注入dao
    @Autowired
    private RoleDao roleDao;

    public PageInfo findAll(int page, int size, String companyId) {
        //调用PageHelper
        PageHelper.startPage(page, size);
        List<Role> list = roleDao.findAll(companyId);
        return new PageInfo(list);
    }

    public List<Role> findAll(String companyId) {
        List<Role> list = roleDao.findAll(companyId);
        return list;
    }

    public Role findById(String id) {
        Role role = roleDao.findById(id);
        return role;
    }

    public void add(Role role) {
        roleDao.add(role);
    }

    public void update(Role role) {
        roleDao.update(role);
    }

    public void delete(String id) {
        roleDao.delete(id);
    }

    //嵌套查询得出用户所具有的所有角色
    public List<Role> findByUserId(String id) {
        List<Role> roleList = roleDao.findByUserId(id);
        return roleList;
    }

    public List<Module> findByRoleId(String id) {
        List<Module> roleList = roleDao.findByRoleId(id);
        return roleList;
    }

    public void deleteRoleModule(String roleid) {
        roleDao.deleteRoleModule(roleid);
    }

    public void addpeRoleModule(String roleid, String s) {
        roleDao.addpeRoleModule(roleid,s);
    }

    public void upDateRoleModule(String roleid, String moduleIds) {
        //先将此角色权限全部清空
        roleDao.deleteRoleModule(roleid);

        //将权限添加至中间表
        String[] strings = moduleIds.split(",");

        for (String s : strings) {
            roleDao.addpeRoleModule(roleid,s);
        }
    }
}
